package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceMapperImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Value("20")
    private Integer maxCount;

    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        // 根据参数uid调用addressMapper的countByUid(Integer uid)方法，统计当前用户的收货地址数据的数量
        Integer count = addressMapper.countByUid(uid);
        // 判断数量是否达到上限值
        if (count > maxCount) {
            // 是：抛出AddressCountLimitException
            throw new AddressCountLimitException("收货地址数量已经达到上限(" + maxCount + ")！");
        }

        // 补全数据：省、市、区的名称
    /* *//*   String provinceName = districtService.getNameByCode(address.getProvinceCode());
        String cityName = districtService.getNameByCode(address.getCityCode());
        String areaName = districtService.getNameByCode(address.getAreaCode());*//*
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);*/

        // 补全数据：将参数uid封装到参数address中
        address.setUid(uid);
        // 补全数据：根据以上统计的数量，得到正确的isDefault值(是否默认：0-不默认，1-默认)，并封装
        Integer isDefault = count == 0 ? 1 : 0;
        address.setIsDefault(isDefault);
        // 补全数据：4项日志
        Date now = new Date();
        address.setCreatedUser(username);
        address.setCreatedTime(now);
        address.setModifiedUser(username);
        address.setModifiedTime(now);

        // 调用addressMapper的insert(Address address)方法插入收货地址数据，并获取返回的受影响行数
        Integer rows = addressMapper.insert(address);
        // 判断受影响行数是否不为1
        if (rows != 1) {
            // 是：抛出InsertException
            throw new InsertExcption("插入收货地址数据时出现未知错误，请联系系统管理员！");
        }
    }


    @Override
    public List<Address> getByUid(Integer uid) {
        List<Address> list = addressMapper.findByUid(uid);
        for (Address address:list)
        {
            //address.setAid(null);
           // address.setUid(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setTel(null);
            address.setIsDefault(null);
            address.setCreatedTime(null);
            address.setCreatedUser(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);
        }
        return list;
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if(result == null)
        {
            throw new AddressNotFoundException("收货地址不存在");
        }
        //检测当前获取到的收货地址数据的归属
        if (!result.getUid().equals(uid))
        {
            throw new AccessDeniedException("非法数据访问");
        }
        Integer rows;
        //先将所有的收获地址设置非默认
        try {
            rows = addressMapper.updateNonDefault(uid);
        }catch (Exception e)
        {

        }finally {
            //将用户选中某条地址设置为默认收货地址
            rows = addressMapper.updateDafaultByAid(aid,username,new Date());
            System.out.println(rows);
            if (rows != 1)
            {
                throw new Updateexception("更新数据产生未知的异常");
            }
        }
        /*Integer rows = addressMapper.updateNonDefault(uid);
        System.out.println(rows);
        if (rows < 1)
        {
            throw new Updateexception("更新数据产生未知的异常");
        }
        //将用户选中某条地址设置为默认收货地址
        rows = addressMapper.updateDafaultByAid(aid,username,new Date());
        if (rows != 1)
        {
            throw new Updateexception("更新数据产生未知的异常");
        }*/
    }
}
